package com.ydy.rod;

import com.alibaba.fastjson.JSONReader;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.github.davidfantasy.jwtshiro.JWTHelper;
import com.github.davidfantasy.jwtshiro.annotation.AlowAnonymous;
import com.ydy.domain.Student;
import com.ydy.service.StudentBiz;
import com.ydy.service.StudentService;
import com.ydy.vo.JsonResult;
import com.ydy.vo.Studentvo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class HelloController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private StudentBiz studentBiz;

    @GetMapping("/")
    public String index() {
        return "Greetings from Spring Boot!";
    }

    @GetMapping("/students")

    public List<Student> hello() {
        return studentService.list();
    }

    @GetMapping("/getStudentByname")
    public List<Student> getStudenByName(String name){
        return  studentService.list(new LambdaQueryWrapper<Student>().eq(Student::getName,name));
    }
    @GetMapping("getStudentByid")
    public Student getStudentByid(Integer id){
        return studentService.getById(id);
    }
    @PostMapping("/inserStudent")
    public void insertStudent(@RequestBody Student student){
        studentBiz.insertStudent(student);
    }
    @PostMapping("/update")
    public void updateStudentByName(@RequestBody Student student){
        studentBiz.updateStudentByName(student);
    }

    @DeleteMapping("/deleteById")
    public void deleteById(Integer id){
        studentBiz.deleteById(id);
    }


    @GetMapping("/getStudentvo")
    public Studentvo getSyudentvo(Integer id ){
        return studentBiz.getStudentvo(id);
    }
    @PostMapping("/addStudent")
    public void  addStudent(@RequestBody Studentvo studentvo){
        studentBiz.addStudent(studentvo);
    }
@GetMapping("/getSrudentvowithRedis")
    public Studentvo getSrudentvowithRedis(String name ){
        return studentBiz.getSrudentvowithRedis(name);
}
@Autowired
    private JWTHelper jwtHelper;
@AlowAnonymous
    @GetMapping("/login")
    public JsonResult login (String name , String password){
    boolean b =studentBiz .checkName(name,password);
    if (!b){
        return JsonResult.fail("账号或密码错误");
    }
    String token = jwtHelper.sign(name,password);
    return JsonResult.suc(token);

}
}
